<?php
namespace Admin\Controller;

use Think\Controller;

class LoginController extends Controller
{
    public function index()
    {
        $this->display('login');
    }


    public function showUserMenu()
    {
        $user = M('user')->select();
        $pagesize = 5;#每页数量
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $count = count($user);
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $dish = M('user')
            ->limit($offset . ',' . $pagesize)
            ->select();
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('page', $page);
        $this->assign('user', $dish);
        $this->display();
    }

    public function addUser()
    {
        $userId = isset($_POST['user_id']) ? intval($_POST['user_id']) : 0;
        if ($userId != 0) {
            $this->assign('user', M('user')->find($userId));
        }

        $this->display();
    }

    public function addUserInfo()
    {
        $userId = isset($_POST['user_id']) ? intval($_POST['user_id']) : 0;
        $username = isset($_POST['username']) ? trim($_POST['username']) : '';
        $password = isset($_POST['password']) ? trim($_POST['password']) : '';
        $password = getMd5Password($password);
        $userInfo['username'] = $username;
        $userInfo['password'] = $password;
        $user = M('user');
        $username1[username] = $username;
        if ($userId == 0) {
            if ($user->where($username1)->find()) {
                $this->error('用户名已经存在');
            } else {
                if ($user->add($userInfo)) {
                    $this->success("添加用户成功");
                } else {
                    $this->error('添加用户失败');
                }
            }
        } else {
            if ($user->where($username1)->find()) {
                $this->error('用户名已经存在');
            } else {
                if ($user->where('id=' . $userId)->save($userInfo)) {
                    $this->success("更新用户成功");
                } else {
                    $this->error('更新用户失败');
                }
            }
        }
    }
        public function getUserInfo(){
            $this->assign('username', $_SESSION["userLogin1"] );
        }

        public function doLogin()
        {
            $username = isset($_POST['username']) ? trim($_POST['username']) : '';
            $password = isset($_POST['password']) ? trim($_POST['password']) : '';
            $userInfo['username'] = $username;
            $userInfo['password'] = getMd5Password($password);
            $user = M('user');
            if ($user->where($userInfo)->find()) {
                if ($user->where($userInfo)->find()['user_type'] == 1) {
                    session('userLogin', $username);
                    $this->display('Index/index');
                } else if ($user->where($userInfo)->find()['user_type'] == 2) {
                    session('userLogin1', $username);
                    $this->assign('username', $username);
                    session('userLogin', $username);
                    $this->assign('username', $username);
                    $this->display('Checkout/checkout');
                }
            } else {
                $this->error('用户账号或密码错误');
            }
        }


    public function checkoutExit()
    {
        $_SESSION["userLogin1"] = null;
        $this->redirect('Login/login');
    }

    public function doDelete()
    {
        $userId = isset($_GET['user_id']) ? intval($_GET['user_id']) : 0;
        if ($userId == 0) {
            $this->error('用户不存在');
        } else {
            if (M('user')->delete($userId)) {
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        }
    }

    public function doexit()
    {
        session('userLogin', null);
        $this->redirect('Login/login');
    }


}